var Background = (function (){
	var tabs_added = {};
	
	var tabs_id_url = {};

	// variables ----------------------------------------------------------------
	var _this 		= {},
		_websites	= [];
			
	// initialize ---------------------------------------------------------------
	_this.init = function (){
		// list of website liked
		_websites = [];
		
		// receive post messages from "inject.js" and any iframes
		chrome.extension.onRequest.addListener(onPostMessage);
		
		// manage when a user change tabs
		chrome.tabs.onActivated.addListener(onTabActivated);

		chrome.browserAction.onClicked.addListener(function(tab) { 
			_this.tell("toggle_iframe_wc");
		});
		
		chrome.tabs.onRemoved.addListener(function (tab_id){
			//console.log("removendo onremove "+tabs_id_url[tab_id]);
			if(tabs_id_url[tab_id] in tabs_added)
				delete tabs_added[tabs_id_url[tab_id]];
		});
		
		chrome.tabs.onCreated.addListener(function (tab){
			tabs_id_url[tab.id] = tab.url;
			//console.log(tab);
		});
		
		chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){
			if('url' in changeInfo){
				//console.log("removendo onupdate "+tabs_id_url[tabId]);
				
				if(tabs_added[tabs_id_url[tabId]])
					delete tabs_added[tabs_id_url[tabId]];
					
				tabs_id_url[tabId] = changeInfo.url;
			}
		});
	};
	
	// private functions --------------------------------------------------------
	function upateCurrentTab (){
		// highlight the "heart" if the web page is already liked
		chrome.tabs.getSelected(null, function (tab){
			var website = null;
			
			for (var i in _websites){
				if (_websites[i].url == tab.url) website = _websites[i];
			}
			
			if (website){
				// send a message to all the views (with "*" wildcard)
				_this.tell('website-is-hearted', {view:'*', comment:website.comment});
			}
		});
	};	
	
	function processMessage (request){
		// process the request
		switch (request.message){
			case 'generate_code_back_ground': message_onGenerateCode(request.data); break;
			case 'add_url': message_onAddUrl(request.data); break;
			case 'all-iframes-loaded': message_allIframesLoaded(request.data); break;
			case 'changed_options': message_changedOptions(request.data); break;
		}
	};
	
	// events -------------------------------------------------------------------
	function onPostMessage (request, sender, sendResponse){
		if (!request.message) return;
		
		// if it has a "view", it resends the message to all the frames in the current tab
		if (request.data.view){
			_this.tell(request.message, request.data);
			return;
		}
		
		processMessage(request);
	};

	function onTabActivated (){
		upateCurrentTab();
	};

	// messages -----------------------------------------------------------------
	function message_onGenerateCode (data){
		_this.tell('generate_code', tabs_added);
	};
	
	function message_changedOptions(data){
		if(data.url.trim() != "") tabs_added[data.url] = data.options;
	}
	
	function message_onAddUrl(data){
		if(data.url.trim() != "")
			if(!(data.url in tabs_added))
			{
				tabs_added[data.url] = { 
					options: {},
					areas: {}
				}
			}
			else
			{
				_this.tell('already_added', {});
			}
	};
	
	function message_allIframesLoaded (data){
		upateCurrentTab();
	};
	
	// public functions ---------------------------------------------------------
	_this.getWebsites = function (){
		return _websites;
	};
	
	_this.tell = function (message, data){
		var data = data || {};
		
		// find the current tab and send a message to "inject.js" and all the iframes
		chrome.tabs.getSelected(null, function (tab){
			if (!tab) return;
			
			chrome.tabs.sendMessage(tab.id, {
				message	: message,
				data	: data
			});
		});
	};
	
	return _this;
}());

window.addEventListener("load", function() { Background.init(); }, false);